let path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var openBrowserWebpackPlugin = require('open-browser-webpack-plugin');
var ExtractTextPlugin = require("extract-text-webpack-plugin");
var definePlugin = new webpack.DefinePlugin({
    __DEV__: (process.env.BUILD_DEV||'dev').trim() == 'dev'
});
// 重写url
function rewriteUrl(replacePath) {
    return function(req,opt) {
        // 取得？
        var queryIndex = req.url.indexOf("?");
        // 取得查询字符串
        var query = queryIndex >= 0 ? req.url.substr(queryIndex) : "";
        //$1取自path匹配到的真实路径中的第一个分组
        //把proxy的path替换为 '/$1\.json',
        req.url = req.path.replace(opt.path, replacePath) + query;
    }
}
module.exports = {
    mode: "none",
    entry: {
      index: path.resolve(__dirname, 'src/index.js'),
      vendor: ['jquery']
    },
    output: {
        path: path.resolve(__dirname, 'build'),
        // a: path.resolve(__dirname,"src/a.js"),
        // b: path.resolve(__dirname,"src/b.js"),
        // filename: 'bundle.js',
        filename: '[name].[hash].js'
    },
    
    plugins: [
      // 从index中分离出来，不再包含在打包出来的index.js
      new webpack.optimize.SplitChunksPlugin('common.js'),
      definePlugin,
      // new ExtractTextPlugin("bundle.css"),
      new HtmlWebpackPlugin({
        title: 'Ken',
        template: "./src/index.html",
        filename: './index.html'
      }),
    //   new HtmlWebpackPlugin({
    //             title:'珠峰Webpack',
    //            template:'./src/index.html',
    //            filename:'./a.html',
    //            chunks:['a','common.js']//包含产出的资源
    //       }),
    // new HtmlWebpackPlugin({
    //             title:'珠峰Webpack',
    //            template:'./src/index.html',
    //             filename:'./b.html',
    //             chunks:['b','common.js']//包含产出的资源
    //         }),
      new openBrowserWebpackPlugin({ url: 'http://localhost:8080' })
    ],
    resolve: {
        // 自动扑圈后缀，注意第一个必须 是空字符串
        extensions: [".js", ".css", ".json"],
    },
    devServer: {
        stats: { colors: true }, // 显示颜色
        port: 8000,
        contentBase: "build", //指定静态目录,
        inline:true, //设置自动刷新
        proxy: [
            {
                // // 替换路径
                // path: /^\/api\/(.*)/,
                // // 目标端口
                // target: "http://localhost:8000/",
                // // 重写路径
                // rewrite: rewriteUrl('/$1\.json'),
                // // 修改来源地址
                // changeOrigin: true,
            }
        ]
    },
    module: {
        rules: [
          {
            test: require.resolve('jquery'),
            use: [{
              loader:  "expose-loader",
              options: 'jQuery'
            },{
              loader: 'expose-loader',
              options: '$'
          }]
          },
          {
            test: /\.css$/,
            use: [
              { loader: 'style-loader' },
              {
                loader: 'css-loader',
                options: {
                  modules: true
                }
              },
              { loader: 'sass-loader' }
              //  ExtractTextPlugin.extract("style-loader"
              //        , "css-loader", "sass-loader")
            
            ]
          },
          // {
          //     test: /\.js$/,
          //     use: [{
          //         loader: "babel-loader"
          //     }]
          // },
          {
            // 解析less
            test: /\.less$/,
            use:[ 'style-loader','css-loader','less-loader'],
            // use: [ ExtractTextPlugin.extract("style-loader"
            //          , "css-loader","less-loader")]
          },
          {
            test: /\.(woff|woff2|ttf|svg|eot)$/,
            use: ["url-loader","file-loader"]
          },
          {
            test: /\.(jpg|png)$/,
            use: ["url-loader","file-loader"]
          }
        ]
      }
}